<?php 
/*************************************************************************************
 * doedittrip.php
 * -----------
 * phpGMapTrips 
 * 
 * This php application allows you to easily record your travel destinations 
 * and display them with Google maps.
 * 
 * Author: Stephane Coste (lescoste@gmail.com)
 * Copyright: (c) 2006 Stephane Coste (http://lescoste.net/)
 * Release Version: 0.1
 * CVS Revision Version: $Revision: 1.11 $
 * Date Started: 2006/02/19
 * Last Modified: $Date: 2007/03/14 14:32:43 $
 *
 *************************************************************************************
 *
 *     This file is part of phpGMapTrips.
 *
 *   phpGMapTrips is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   phpGMapTrips is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with phpGMapTrips; if not, write to the Free Software
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 ************************************************************************************/
?>
<?PHP
include_once("secure.php");
/*
this page updates or create a trip in the xml file
<trips>
<trip name="Africa" groups="africa; europe">
<point name="" seq="1" /> // if the name is filled use it to find the lat/long else use lat/long
<point seq="2" lat="" long=""/>
<point name="" seq="3" />
</trip>
</trips>
*/

$pointorder = urldecode($_POST["pointorder"]);
parse_str($pointorder);
$showtrip = $_POST["showtrip"];
$tripgroups = $_POST["tripgroups"];
$file = urldecode($_GET["file"]);
if (empty($f_files)) {
   $file = $_POST["file"];
}

?>
<html>
<head>
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<style>
body {
	background:white;
	font-size:xx-small;
	color:gainsboro;
}
</style>
</head>
<body>
<?php 
include_once("utils.php");
include_once("dbaccess.php");


$noproblemo = true;
$hasBeenSaved = false;

//print_r($edittriplist);

$tmpfilename = $f_trips.rand().".tmp";
$fpw = fopen($tmpfilename, "w");
if (flock($fpw, LOCK_EX)) { // do an exclusive lock
	fwrite($fpw, '<?xml version="1.0"?>');
	fwrite($fpw, '<trips>');
	fwrite($fpw, "\r\n");

	if (empty($showtrip) || empty($pointorder)) {
		echo $lang_error_tryagain;
		$noproblemo = false;
	} else {
		$hasBeenSaved = saveTripPointsOrder($fpw, $hasBeenSaved, $showtrip, $edittriplist, $tripgroups);
		if (!$hasBeenSaved) {
			// add new trip
			echo '<br/>'.$lang_place.' '.$showtrip.' '.$lang_place_added.'.';
			fwrite($fpw, printTrip($showtrip, $tripgroups, array(), $edittriplist));
			$hasBeenSaved = true;
		}

		fwrite($fpw, '</trips>');
		flock($fpw, LOCK_UN); // release the lock
		fclose($fpw);  
		copy($f_trips, $f_trips.'.old');
		copy($tmpfilename, $f_trips);
		unlink($tmpfilename);
	}
} else {
	echo "Couldn't lock the file !";
	fclose($fpw);  
}

//($showtrip, $tripgroups, $opttripseqarray, $pointorder, );
function printTrip($tripname, $tripgroups, $points, $pointorder) {
	global $index, $showtrip;
	$r = "";
	$r .= "<trip name=\"".$tripname."\" groups=\"".$tripgroups."\">";
	if (empty($points)) {
		// first time not ordered yet
		$pointstmp = getTripPoints($tripname);
		//print_r($pointstmp);
		ksort($pointstmp);
		reset($pointstmp);
		$i = 0;
		$points = array();
		foreach($pointstmp as $point) {
			$points[$i] = $point;
			$i++;
		}
	} else {
		// make sure we see all the points even the new ones that haven't been ordered before
		if (count($pointorder)>0) {
			$tagnames = getTripOptionalData($tripname);
			$pointstmp = getTripPoints($tripname);
			foreach($pointstmp as $pointname => $point) {
				if (!isset($tagnames[$tripname][$pointname])) {
					array_push($points, $point);
				}
			}
			// make sure that we have not more points : remove removed points
			$tmppoints = array();
			$ip = 0;
			foreach($points as $pointor => $pointar) {
				if (isset($pointstmp[$pointar["name"]])) {
					$tmppoint = array();
					$tmppoint["name"] = $pointar["name"];
					$tmppoint["seq"] = $ip;
					$tmppoints[$ip] = $tmppoint;
					$ip++;
				}
			}
			$points = $tmppoints;
		}
	}
	if (empty($pointorder)) {
		$array_count = count($points);
		for($y=0; $y<$array_count; $y++) {
			$r .= printPoint($y, $points[$y]);
		}
	} else {
		$array_count = count($pointorder);
		for($y=0; $y<$array_count; $y++) {
			$r .= printPoint($y, $points[$pointorder[$y]]);
		}
	}

	$r .= "</trip>\n";
	$index++;
	return $r;
}
function printPoint($seq, $point) {
	$r = "";
	//$r .= '<point name="'.$destiName.'" lat="'.$destiLat.'" long="'.$destiLng.'"  seq="'.$seq.'"/>';
	$r .= "<point name=\"".$point["name"]."\" seq=\"".$seq."\"/>";
	return $r;
}

if ($noproblemo) {
?>
<script>
parent.callTrip('<?php echo $file; ?>', '<?php echo $showtrip; ?>');
</script>
<?php } ?>
<br/>
<a href="javascript:void(0);" onclick="parent.callTrip('<?php echo $file; ?>', '<?php echo $showtrip; ?>');">Back</a>
</body>
</html>
